package com.kaixinff.kaixin001.servlet;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaixinff.kaixin001.util.Filter;
import com.kaixinff.kaixin001.util.KXClient;
import com.kaixinff.kaixin001.util.KXUtil;
import com.kaixinff.kaixin001.util.RegStep;
import com.kaixinff.name.User;

@SuppressWarnings("serial")
public class InfoServlet extends HttpServlet {
	private static final String URL = KXUtil.XW_URL + "/forecast_sea.php";
	private static final String INDEX_URL = KXUtil.XW_URL + "/index.php";
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html; charset=" + KXUtil.CHARSET);
		List<User> users = KXUtil.getUsers(new Filter(){
			@Override
			public boolean access(User user) {
				return user.hasStep(RegStep.ADD_XW);
			}}, resp);
		if (users == null) {
			return;
		}
		
		resp.getWriter().println("<table border=1><tr><td></td><td>用户</td><td>姓名</td><td>等级</td><td>能量</td><td>人数</td><td>金币</td><td>升级经验</td></tr>");
		int index = 0;
		for (User user: users) {
			getInfo(resp, user, ++index);
		}
		resp.getWriter().println("</table>");
	}
	
	private void getInfo(HttpServletResponse resp, User user, int index) throws IOException {
		KXClient client = KXClient.getInstance(user.getId());
		resp.getWriter().println("<tr><td>");
		resp.getWriter().println(index);
		resp.getWriter().println("</td><td>");
		resp.getWriter().println(user.getId());
		resp.getWriter().println("</td><td>");
		resp.getWriter().println(user.getName());
		resp.getWriter().println("</td><td>");
		String cont = client.doGet(INDEX_URL, null).getContent();
		Pattern p = Pattern.compile("<span id=\"user_level\">(\\d+?)</span>");
		Matcher m = p.matcher(cont);
		if (m.find()) {
			resp.getWriter().println(m.group(1));
		} else {
			cont = client.doGet(INDEX_URL, null).getContent();
			p = Pattern.compile("<span id=\"user_level\">(\\d+?)</span>");
			m = p.matcher(cont);
			if (m.find()) {
				resp.getWriter().println(m.group(1));
			} else {
				resp.getWriter().println("?");
			}
		}
		resp.getWriter().println("级</td><td>");
		
		p = Pattern.compile("XW[.]processTimer[(]XW[.]ajaxNum,\"energy\",[-|\\d]+?,(\\d+?),(\\d+?),(\\d+?),(\\d+?),");
		m = p.matcher(cont);
		if (m.find()) {
			int energy = Integer.parseInt(m.group(1));
			if (energy > 80) {
				resp.getWriter().println("<span style='color:red'>" + energy + "</span>" + " / " + m.group(2));
			} else {
				resp.getWriter().println(energy + " / " + m.group(2));
			}
			
		} else {
			resp.getWriter().println("?");
		}
		
		resp.getWriter().println("</td><td>");
		
		p = Pattern.compile("\"teamnum\":(\\d+?),");
		m = p.matcher(cont);
		if (m.find()) {
			resp.getWriter().println(m.group(1));
		} else {
			resp.getWriter().println("?");
		}
		resp.getWriter().println("</td><td>");
		p = Pattern.compile("\"suncoin\":\"(\\d+?)\"");
		m = p.matcher(cont);
		if (m.find()) {
			int suncoin = Integer.parseInt(m.group(1));
			if (suncoin >= 100) {
				resp.getWriter().println("<span style='color:green'>" + suncoin + "</span>");
			} else {
				resp.getWriter().println(suncoin);
			}
		} else {
			resp.getWriter().println("0");
		}
		resp.getWriter().println("</td><td>");
		
		/*if (cont.indexOf("挑战狂人托尼") > -1) {
			resp.getWriter().println("<span style='color:red'> X </span>");
		} else {
			resp.getWriter().println(" V ");
		}
		if (cont.indexOf("购买宝石") > -1) {
			resp.getWriter().println("<span style='color:red'> X </span>");
		} else {
			resp.getWriter().println(" V ");
		}
		if (cont.indexOf("收割橡胶") > -1) {
			resp.getWriter().println("<span style='color:red'> X </span>");
		} else {
			resp.getWriter().println(" V ");
		}
		if (cont.indexOf("继续做任务") > -1) {
			resp.getWriter().println("<span style='color:red'> X </span>");
		} else {
			resp.getWriter().println(" V ");
		}
		resp.getWriter().println("</td><td>");
		*/
		
//		cont = client.doGet(KXUtil.XW_URL + "/profile.php", null).getContent();
//		p = Pattern.compile("/50mgun\\.gif\"><br>×(\\d+?)</td>");
//		m = p.matcher(cont);
//		int num = 0;
//		if (m.find()) {
//			num = Integer.parseInt((m.group(1)));
//		}
//		if (num >= 2) {
//			resp.getWriter().println("<span style='color:green'> ×" + num + " </span>");
//		} else {
//			resp.getWriter().println("<span style='color:red'> ×" + num + " </span>");
//		}
		p = Pattern.compile("title=\"再有(\\d+?)经验值可升到");
		m = p.matcher(cont);
		if (m.find()) {
			resp.getWriter().println(m.group(1));
		}
		
		resp.getWriter().println("</td></tr>");
		resp.getWriter().flush();
	}
	
}
